package com.example.demo.web.rest.controller;

import com.example.demo.client.evt.sysbasequery.AddibleArticleEvt;
import com.example.demo.client.evt.sysbasequery.QueryArticleCatalogEvt;
import com.example.demo.client.vm.sysbasequery.AddibleArticleVm;
import com.example.demo.client.vm.sysbasequery.CatalogTreeVm;
import com.example.demo.client.vm.sysbasequery.QueryArticleVm;
import com.ffcs.oss.param.out.ServiceResp;
import com.ffcs.oss.param.vm.QueryPageVm;
import com.example.demo.service.CatalogService;
import com.example.demo.web.rest.evt.ArticleCatalogIdEvt;
import com.example.demo.web.rest.evt.IdEvt;
import com.example.demo.web.rest.evt.RelationEvt;
import com.example.demo.web.rest.evt.SaveCatalogEvt;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 帮助目录管理
 * @Author zhengmf
 * @Date 2019/1/8  16:00
 */
@ApiModel(description ="帮助目录接口")
@RestController
@RequestMapping(value = "/api/catalog")
public class CatalogController extends BaseController{
    @Autowired
    private CatalogService catalogService;

    @ApiOperation("保存目录信息")
    @PostMapping(value = "/saveCatalog")
    public ServiceResp<Boolean> saveCatalog(@RequestBody @Validated SaveCatalogEvt evt){
        catalogService.saveCatalog(evt);
        return ServiceResp.getInstance(Boolean.class).success(true);
    }
    @ApiOperation("删除帮助目录")
    @PostMapping(value = "/delCatalog")
    public ServiceResp<Boolean> delCatalog(@RequestBody @Validated IdEvt evt){
        return catalogService.delCatalog(evt.getId());
    }

    @ApiOperation("查询可添加文章查询列表参数")
    @PostMapping("/queryAddibleArticles")
    public ServiceResp<QueryPageVm<AddibleArticleVm>> queryAddibleArticles(@RequestBody AddibleArticleEvt evt){
        return baseQueryClient.queryAddibleArticles(evt);
    }

    @ApiOperation("获取目录树")
    @PostMapping("/getCatalogTree")
    public ServiceResp<List<CatalogTreeVm>> getCatalogTree(){
        return baseQueryClient.getCatalogTree();
    }

    @ApiOperation("帮助目录管理查询文章")
    @PostMapping("/queryArticle")
    public ServiceResp<QueryPageVm<QueryArticleVm>> queryArticle(@RequestBody QueryArticleCatalogEvt evt){
        return baseQueryClient.queryArticle(evt);
    }

    @ApiOperation("保存文章目录关系")
    @PostMapping("/addArticleCatalog")
    public ServiceResp<Boolean> addArticleCatalog(@RequestBody @Validated RelationEvt evt){
        catalogService.addArticleCatalog(evt);
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

    @ApiOperation("删除文章目录关系")
    @PostMapping("/delArticleCatalog")
    public ServiceResp<Boolean> delArticleCatalog(@RequestBody @Validated ArticleCatalogIdEvt evt){
        if(!baseQueryClient.hasRelation(evt).getBody()){
            return ServiceResp.getInstance(Boolean.class).success(false,"请到所属目录删除文章！");
        }
        catalogService.delArticleCatalog(evt);
        return ServiceResp.getInstance(Boolean.class).success(true,"删除成功！");
    }

}
